package com.wzq.util;

import java.text.SimpleDateFormat;
import java.util.Date;


public class DateUtil {

	/**
	 * 日期对象转字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String formatDate(Date date,String format){
		String result="";
		SimpleDateFormat sdf=new SimpleDateFormat(format);

		if(date!=null){
			result=sdf.format(date);
		}
		return result;
	}

	//获取当前的日期（自定义格式）
	public static String getCurrentDatePath(String format)throws Exception{
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * 字符串转日期对象
	 * @param str
	 * @param format
	 * @return
	 * @throws Exception
	 */
	public static Date formatString(String str,String format) throws Exception{
		if(StringUtil.isEmpty(str)){
			return null;
		}
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		return sdf.parse(str);
	}

	public static String getCurrentDateStr(){
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmssSSSSSSSSS");
		return sdf.format(date);
	}

	public static String getCurrentDatePath()throws Exception{
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		return sdf.format(date);
	}

	public static void main(String[] args) {
		try {
			System.out.println(getCurrentDateStr());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//日期格式小改
	public static String changetime(String time){
		String[] split = time.split("月|年");
		if (split[1].length()<2) split[1]="0" +split[1];
		return split[0] + "-" + split[1];
	}
	//hh:mm - hh:mm 半小时为单位的换算
	public static String[] getNumber(String time) {
		String[] split = time.split("-");
		int s=( Integer.parseInt( split[1].substring(0,2) )- Integer.parseInt( split[0].substring(0,2) ) ) *2 +
				( Integer.parseInt( split[1].substring(3))- Integer.parseInt( split[0].substring(3)) ) /30;
		String[] times = new String[s+1] ;
		int i=0;
		times[i++]= split[0];
		while (!split[0].equals(split[1])){
			if ( !"00".equals(split[0].substring(3))){
				if ( Integer.parseInt(split[0].substring(0,2))+1<10 )
					split[0] ="0"+ (Integer.parseInt(split[0].substring(0,2))+1) + ":00";
				else
					split[0] = (Integer.parseInt(split[0].substring(0,2))+1) + ":00";
			}else {
				split[0] = split[0].substring(0,3) + "30";
			}
			times[i++] =split[0];
		}
		return  times;
	}
	public static int month_days(int year,int month) {
		int d = 0;
		switch (month) {
			case 1:
				d = 31;
				break;
			case 2:
				d = 28 + (((0 == year % 4 && year % 100 != 0) || (year % 400 == 0)) ? 1 : 0);
				break;
			case 3:
				d = 31;
				break;
			case 4:
				d = 30;
				break;
			case 5:
				d = 31;
				break;
			case 6:
				d = 30;
				break;
			case 7:
				d = 31;
				break;
			case 8:
				d = 31;
				break;
			case 9:
				d = 30;
				break;
			case 10:
				d = 31;
				break;
			case 11:
				d = 30;
				break;
			case 12:
				d = 31;
				break;
		}
		return d;
	}
}
